package com.ybkj.o2o.zhuanche.controller;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.zhuanche.common.query.CarTypeQuery;
import com.ybkj.o2o.zhuanche.common.transport.Error;
import com.ybkj.o2o.zhuanche.common.transport.Result;
import com.ybkj.o2o.zhuanche.entity.CarType;
import com.ybkj.o2o.zhuanche.manager.CarTypeManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/carType")
public class CarTypeRes {

  @Autowired
  private CarTypeManager carTypeManager;
  
  /**
   * 查询车型.
   * @param companyName 公司名称
   * @param page 分页参数
   * @param rows 分页参数
   */
  @RequestMapping(value = "findByCompanyCarType", method = RequestMethod.GET)
  public ResponseEntity<Object> findByCompanyCarType(
      @RequestParam(required = false, value = "companyName") String companyName,
      @RequestParam(required = false, value = "page") Integer page,
      @RequestParam(required = false, value = "rows") Integer rows,
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    ResponseEntity<Object> responseEntity = null;
    
    CarTypeQuery carTypeQuery = new CarTypeQuery();
    carTypeQuery.setCompanyName(companyName);
    carTypeQuery.setPage(page);
    carTypeQuery.setRows(rows);
    carTypeQuery.setAppKey(appKey);
    
    try {
      
      List<CarType> list = carTypeManager.findByParam(carTypeQuery);

      Result<List<CarType>> result = new Result<List<CarType>>(0, "成功", list);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }
  
  /**
   * 统计公司车型数量.
   * @param companyName 公司名称
   */
  @RequestMapping(value = "countByCompanyCarType", method = RequestMethod.GET)
  public ResponseEntity<Object> countByCompanyCarType(
      @RequestParam(required = false, value = "companyName") String companyName, 
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    ResponseEntity<Object> responseEntity = null;
    
    CarTypeQuery carTypeQuery = new CarTypeQuery();
    carTypeQuery.setCompanyName(companyName);
    carTypeQuery.setAppKey(appKey);
    try {

      Long data = carTypeManager.countByCompanyName(carTypeQuery);
      
      Result<Long> result = new Result<Long>(0, "成功", data);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

  /**
   * 查询汽车类型.
   */
  @RequestMapping(value = "findCarType", method = RequestMethod.GET)
  public ResponseEntity<Object> findCarType(
      @RequestParam(required = false, value = "companyId") Long companyId,
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    ResponseEntity<Object> responseEntity = null;
    
    try {
      
      List<CarType> list = carTypeManager.findAll(companyId, appKey);
      Result<List<CarType>> result = new Result<List<CarType>>(0, "成功", list);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
      
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }


  /**
   * 查询单个汽车类型.
   * @param carTypeId 车型id
   */
  @RequestMapping(value = "findByCarTypeId", method = RequestMethod.GET)
  public ResponseEntity<Object> findByCarTypeId(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(carTypeId != null && carTypeId != 0, "汽车类型Id不能为空");
    
    try {
      
      CarType carType = carTypeManager.findByCarTypeId(carTypeId, appKey);
      Result<CarType> result = new Result<CarType>(0, "成功", carType);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
      
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 创建汽车类型.
   * @param typeName 车型名称
   * @param areaId 收费id
   * @param sequence 顺序
   * @param picture 图片
   * @param appKey 系统可以
   */
  @RequestMapping(value = "create", method = RequestMethod.POST)
  public ResponseEntity<Object> create(
      @RequestParam("typeName") String typeName,
      @RequestParam("areaId") Long areaId,
      @RequestParam("sequence") Integer sequence,
      @RequestParam(required = false, value = "picture") String picture,
      @RequestParam("companyId") Long companyId,
      @RequestParam("companyName") String companyName,
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(typeName), "typeName不能为空");
    Preconditions.checkArgument(companyId != null, "公司Id不能为空");
    ResponseEntity<Object> responseEntity = null;
    CarTypeQuery carTypeQuery = new CarTypeQuery();
    carTypeQuery.setTypeName(typeName);
    carTypeQuery.setCompanyId(companyId);
    carTypeQuery.setAppKey(appKey);
    try {
      Long count = carTypeManager.countByParams(carTypeQuery);
      if (!count.equals(0L)) {
        Error error = new Error(500, "名称重复,添加失败");
        responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
        return responseEntity;
      }
      CarType carType = new CarType();
      carType.setAppKey(appKey);
      carType.setAreaId(areaId);
      carType.setPicture(picture);
      carType.setTypeName(typeName);
      carType.setSequence(sequence);
      carType.setCompanyId(companyId);
      carType.setCompanyName(companyName);
      CarType type = carTypeManager.save(carType);
      Result<CarType> result = new Result<CarType>(0, "成功", type);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }


  /**
   * 修改汽车类型.
   * @param carTypeId 车型id
   * @param typeName 车型名称
   * @param areaId 收费id
   * @param sequence 顺序
   * @param picture 图片
   */
  @RequestMapping(value = "update", method = RequestMethod.GET)
  public ResponseEntity<Object> update(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam(value = "typeName", required = false) String typeName, 
      @RequestParam(value = "areaId", required = false) Long areaId,
      @RequestParam(value = "sequence", required = false) Integer sequence,
      @RequestParam(value = "picture", required = false) String picture, 
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(carTypeId != null, "carTypeId不能为空");
    ResponseEntity<Object> responseEntity = null;
    try {
      CarType carType = carTypeManager.findByCarTypeId(carTypeId, appKey);
      if (carType != null) {
        carType.setUpdated(new Date());
        if (areaId != null) {
          carType.setAreaId(areaId);
        }
        if (StringUtils.isNotBlank(typeName)) {
          carType.setTypeName(typeName);
        }
        if (sequence != null) {
          carType.setSequence(sequence);
        }
        if (StringUtils.isNotBlank(picture)) {
          carType.setPicture(picture);
        }
        carTypeManager.update(carType);
      }

      Result<Object> result = new Result<Object>(0, "成功", null);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }


  /**
   * 删除汽车类型.
   * @param carTypeId 车型id
   */
  @RequestMapping(value = "delete", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(
      @RequestParam("carTypeId") Long carTypeId,
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(carTypeId != null, "carTypeId不能为空");
    ResponseEntity<Object> responseEntity = null;
    try {
      
      CarType carType = carTypeManager.findByCarTypeId(carTypeId, appKey);
      Preconditions.checkArgument(carType != null, "车辆类型不存在");
      carTypeManager.deleteById(carType.getId(), carType.getAppKey());
      Result<Object> result = new Result<Object>(0, "成功", null);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
      
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

  @RequestMapping(value = "findByAreaId", method = RequestMethod.GET)
  public ResponseEntity<Object> findByAreaId(
      @RequestParam("areaId") Long areaId,
      @RequestParam("appKey") String appKey) {
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    ResponseEntity<Object> responseEntity = null;

    try {
      List<CarType> list = carTypeManager.findByAreaId(areaId, appKey);
      Result<List<CarType>> result = new Result<List<CarType>>(0, "成功", list);
      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);
      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }
  }

}
